package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.ArtfileMapper;
import com.hbnu.system.model.entity.Art;
import com.hbnu.system.model.entity.Artfile;
import com.hbnu.system.service.IArtService;
import com.hbnu.system.service.IArtfileService;
import com.hbnu.system.service.IUserService;
import com.hbnu.system.utils.CheckRoleUtil;
import com.hbnu.system.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.BindException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * (Paperfile)表服务实现类
 *
 * @author makejava
 * @since 2023-08-15 13:46:42
 */
@Service("artfileService")
public class ArtfileServiceImpl extends ServiceImpl<ArtfileMapper, Artfile> implements IArtfileService {
    @Autowired
    FileUploadUtil fileUploadUtil;

    @Autowired
    IArtService iArtService;

    @Autowired
    IUserService iUserService;
    @Autowired
    private CheckRoleUtil checkRoleUtil;
    @Override
    public boolean addArtfile(Artfile artfile) {
        Art byId = iArtService.getById(artfile.getArtid());
        checkRoleUtil.checkPermissionAchievement(byId.getStatus());
        artfile.setFiledate(LocalDateTime.now());
        if(baseMapper.insert(artfile) != 1)
            throw new BaseException("上传失败,请重新尝试");
        return true;
    }

    @Override
    public Artfile getArtfile(String id) {
        Artfile artfile = super.getOne(Wrappers.lambdaQuery(Artfile.class)
                .eq(Artfile::getFileid,id));
        if(Objects.isNull(artfile)){
            throw new BaseException("文件编号异常!未查找到该文件信息");
        }
        return artfile;
    }

    @Override
    public List<Artfile> pageArtfile(String id) {
        if(StrUtil.isBlank(id)){
            throw new BaseException("艺术作品编号为空值");
        }
        List<Artfile> artfileList = baseMapper.selectList(Wrappers.lambdaQuery(Artfile.class)
                .eq(Artfile::getArtid,id));
        return artfileList;
    }

    @Override
    public boolean deletArtfile(String id) throws IOException {
        Artfile one = super.getOne(Wrappers.lambdaQuery(Artfile.class)
                .eq(Artfile::getFileid, id));
        Art byId = iArtService.getById(one.getArtid());
        checkRoleUtil.checkPermissionAchievement(byId.getStatus());

        if (Objects.isNull(one)){
            throw new BaseException("文件编号有误!");
        }
//        //做flag的判断
//        if(one.getFlag() == 0){
//            throw new BaseException("不可删除该文件!");
//        }

        //判断该艺体状态是否可删
        Art art = iArtService.getById(one.getArtid());
        if(art.getStatus().contains("审核")){
            throw new BindException("该状态不可删除文件!");
        }

        boolean delete = fileUploadUtil.delete(one.getFileurl());
        if(!delete){
            throw new BindException("附件删除失败");
        }
        return baseMapper.deleteById(id) == 1;
    }

    @Override
    public boolean updateArtfile(Artfile artfile) {
        Art byId = iArtService.getById(artfile.getArtid());
        checkRoleUtil.checkPermissionAchievement(byId.getStatus());
        return baseMapper.updateById(artfile)==1;
    }
    



}

